package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcAttendBaseMapper;
import com.lxmes.hr.domain.NcAttendBase;
import com.lxmes.hr.service.INcAttendBaseService;
import javax.validation.Validator;

/**
 * 考勤基数Service业务层处理
 *
 * @author zhangzhiyu
 * @date 2023-02-17
 */
@Service
public class NcAttendBaseServiceImpl implements INcAttendBaseService
{
    @Autowired
    private NcAttendBaseMapper ncAttendBaseMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询考勤基数
     *
     * @param id 考勤基数主键
     * @return 考勤基数
     */
    @Override
    public NcAttendBase selectNcAttendBaseById(Long id)
    {
        return ncAttendBaseMapper.selectNcAttendBaseById(id);
    }

    /**
     * 查询考勤基数列表
     *
     * @param ncAttendBase 考勤基数
     * @return 考勤基数
     */
    @Override
    public List<NcAttendBase> selectNcAttendBaseList(NcAttendBase ncAttendBase)
    {
        return ncAttendBaseMapper.selectNcAttendBaseList(ncAttendBase);
    }

    /**
     * 新增考勤基数
     *
     * @param ncAttendBase 考勤基数
     * @return 结果
     */
    @Override
    public int insertNcAttendBase(NcAttendBase ncAttendBase)
    {
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncAttendBase.setGcCode(ncOrgManages.get(0).getJtdm());
            ncAttendBase.setGcName(ncOrgManages.get(0).getJtdm());
        }
        ncAttendBase.setCreateBy(SecurityUtils.getUsername());
        ncAttendBase.setCreateTime(DateUtils.getNowDate());
        return ncAttendBaseMapper.insertNcAttendBase(ncAttendBase);
    }

    /**
     * 修改考勤基数
     *
     * @param ncAttendBase 考勤基数
     * @return 结果
     */
    @Override
    public int updateNcAttendBase(NcAttendBase ncAttendBase)
    {
        ncAttendBase.setUpdateTime(DateUtils.getNowDate());
        ncAttendBase.setUpdateBy(SecurityUtils.getUsername());
        return ncAttendBaseMapper.updateNcAttendBase(ncAttendBase);
    }

    /**
     * 批量删除考勤基数
     *
     * @param ids 需要删除的考勤基数主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendBaseByIds(Long[] ids)
    {
        return ncAttendBaseMapper.deleteNcAttendBaseByIds(ids);
    }

    /**
     * 删除考勤基数信息
     *
     * @param id 考勤基数主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendBaseById(Long id)
    {
        return ncAttendBaseMapper.deleteNcAttendBaseById(id);
    }
    @Override
    public String imporIteminfo(List<NcAttendBase> ncAttendBaseList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncAttendBaseList) || ncAttendBaseList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAttendBase ncAttendBase : ncAttendBaseList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncAttendBase);
                ncAttendBase.setCreateBy(operName);
                ncAttendBase.setUpdateBy(operName);
                this.insertNcAttendBase(ncAttendBase);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
